//根据key获取对象值
export const getObj = (key, obj) => {
  let value = obj;
  if (key.indexOf(".") > -1) {
    const keysArr = key.split(".");
    for (let i = 0; i < keysArr.length; i++) {
      const k = keysArr[i];
      value = value[k];
    }
  } else {
    value = value[key];
  }
  return value;
};

//数据深拷贝
export const cloneDeep = obj => {
  var result = Array.isArray(obj) ? [] : {};
  for (var key in obj) {
    if (obj.hasOwnProperty(key)) {
      if (typeof obj[key] === "object" && obj[key] !== null) {
        result[key] = cloneDeep(obj[key]); //递归复制
      } else {
        result[key] = obj[key];
      }
    }
  }
  return result;
};

//获取数据,测试用
export const getDataMethod = obj => {
  console.log(obj);
  return new Promise(function(reslove, reject) {
    setTimeout(function() {
      let result = [];
      for (let i = 0; i < 10; i++) {
        result.push({
          Id: i,
          Text: `John Brown${i}`,
          age: 10 + i,
          address: "New York No. 1 Lake Park"
        });
      }
      reslove({
        Data: {
          Datas: result,
          Total: 10
        }
      });
      if (!result.length) {
        reject();
      }
    }, 1000);
  });
};
